import { defineStore } from 'pinia';
// import { useUiStore } from './uiStore'; // Potentially for error handling

export const useWhiteboardStore = defineStore('whiteboard', {
  state: () => ({
    items: [], // WhiteboardItemModel[]
  }),
  getters: {
    getItemById: (state) => {
      return (itemId) => state.items.find(item => item.reference_id === itemId);
    },
  },
  actions: {
    setItems(items) {
      this.items = items || [];
    },
    addItem(item) {
      if (item && !this.items.find(i => i.reference_id === item.reference_id)) {
        this.items.push(item);
      }
    },
    removeItemById(itemId) {
      this.items = this.items.filter(item => item.reference_id !== itemId);
    },
    async addWhiteboardReference(filePath) {
      // const uiStore = useUiStore(); // Uncomment if using uiStore for global errors
      // uiStore.setAppError(null);
      if (!filePath || !filePath.trim()) {
        console.error('File path cannot be empty.');
        // uiStore.setAppError('File path cannot be empty.');
        return null;
      }
      try {
        const response = await window.pywebview.api.add_whiteboard_reference(filePath);
        if (response.success && response.data && response.data.new_item) {
          this.addItem(response.data.new_item); // Add to state after successful backend operation
          return response.data.new_item;
        } else {
          console.error('Failed to add whiteboard reference:', response.error_message);
          // uiStore.setAppError(response.error_message || 'Failed to add whiteboard reference.');
          return null;
        }
      } catch (error) {
        console.error('Error adding whiteboard reference:', error);
        // uiStore.setAppError(`Error adding whiteboard reference: ${error.message || 'Unknown error'}`);
        return null;
      }
    },
    async removeWhiteboardReference(itemId) {
      // const uiStore = useUiStore(); // Uncomment if using uiStore for global errors
      // uiStore.setAppError(null);
      if (!itemId) {
        console.error('Item ID cannot be empty.');
        // uiStore.setAppError('Item ID cannot be empty.');
        return false;
      }
      try {
        const response = await window.pywebview.api.remove_whiteboard_reference(itemId);
        if (response.success) {
          this.removeItemById(itemId); // Remove from state after successful backend operation
          return true;
        } else {
          console.error('Failed to remove whiteboard reference:', response.error_message);
          // uiStore.setAppError(response.error_message || 'Failed to remove whiteboard reference.');
          return false;
        }
      } catch (error) {
        console.error('Error removing whiteboard reference:', error);
        // uiStore.setAppError(`Error removing whiteboard reference: ${error.message || 'Unknown error'}`);
        return false;
      }
    },
    // Initial whiteboard items fetching is handled in App.vue
    // and then setItems is called.
  },
});